from django import forms
from django.contrib.auth import authenticate,get_user_model,login,logout
from django.shortcuts import redirect

User = get_user_model()

class UserLoginForm(forms.Form):
    username = forms.CharField()
    password = forms.CharField(widget=forms.PasswordInput)

    def clean(self, *args, **kwargs):
        username = self.cleaned_data.get("username")
        password = self.cleaned_data.get("password")
        user = authenticate(username=username,password=password)
        # user_qs = User.objects.filter(username=username)
        # if user_qs.count()==1:
        #     user = user_qs.first()
        if username and password:
            user = authenticate(username=username,password=password)
            if not user:
                raise forms.ValidationError("This user does not exist")
            if not user.check_password(password):
                raise forms.ValidationError("Incorrect password")
            if not user.is_active:
                raise forms.ValidationError("This user is not longer active or exist")

        return super(UserLoginForm,self).clean(*args,**kwargs)

class UserRegisterForm(forms.ModelForm):
    password = forms.CharField(widget=forms.PasswordInput)
    password2 = forms.CharField(widget=forms.PasswordInput,label='Confirm Password')
    email = forms.EmailField(label='Email address')
    email2 = forms.EmailField(label='Confirm Email')
    class Meta:
        model = User
        fields = [
            'username',
            'email',
            'email2',
            'password',
            'password2'

        ]

    def clean(self,*args,**kwargs):
        email = self.cleaned_data.get('email')
        email2 = self.cleaned_data.get('email2')
        print(email, email2)
        if email != email2:
            raise forms.ValidationError("Email address not match")
        email_qs = User.objects.filter(email=email)
        if email_qs.exists():
            raise forms.ValidationError("Email already been registered")

        password = self.cleaned_data.get('password')
        password2 = self.cleaned_data.get('password2')
        print(password, password2)
        if password != password2:
            raise forms.ValidationError("Password not match")

        return super(UserRegisterForm,self).clean(*args,**kwargs)


    # def clean_email(self):
    #     email = self.cleaned_data.get('email')
    #     email2 = self.cleaned_data.get('email2')
    #     print(email,email2)
    #     if email != email2:
    #         raise forms.ValidationError("Email address not match")
    #     email_qs = User.objects.filter(email=email)
    #     if email_qs.exists():
    #         raise forms.ValidationError("Email already been registered")
    #
    #     return email
    #
    # def clean_password(self):
    #     password = self.cleaned_data.get('password')
    #     password2 = self.cleaned_data.get('password2')
    #     print(password,password2)
    #     if password != password2:
    #         raise forms.ValidationError("Password not match")
    #     return password

